<html>
<head><meta charset="utf-8"><title>subslices in const · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html">subslices in const</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="247814432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247814432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247814432">(Jul 31 2021 at 17:02)</a>:</h4>
<p>Hey, is it currently possible in any way to create a subslice of a <code>&amp;'static [T]</code> at <code>const</code> time? Currently I get an error.</p>
<div class="codehilite"><pre><span></span><code>error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  --&gt; src/types/tag.rs:26:45
   |
26 |                     if Self::contains(tag, &amp;tags[index+1..tags.len()]) {
   |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^
</code></pre></div>



<a name="247818109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818109">(Jul 31 2021 at 18:29)</a>:</h4>
<p>I thought maybe <code>from_raw_parts</code>, but even that is not <code>const</code></p>



<a name="247818234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818234">(Jul 31 2021 at 18:31)</a>:</h4>
<p><code>ptr::slice_from_raw_parts</code> is <code>const</code>, at least</p>



<a name="247818390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818390">(Jul 31 2021 at 18:35)</a>:</h4>
<p>But not stable <code>const</code> yet</p>



<a name="247818772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818772">(Jul 31 2021 at 18:44)</a>:</h4>
<p>It's possible but hacky. You can use a <code>match</code> pattern to keep removing parts from the beginning and end until you have the right slice.</p>



<a name="247818864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818864">(Jul 31 2021 at 18:46)</a>:</h4>
<p>The <code>konst</code> crate has a function for it: <a href="https://docs.rs/konst/0.2.5/konst/slice/fn.slice_range.html">https://docs.rs/konst/0.2.5/konst/slice/fn.slice_range.html</a></p>



<a name="247818884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247818884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247818884">(Jul 31 2021 at 18:47)</a>:</h4>
<p>Oh wow, I didn't think of using slice patterns for that</p>



<a name="247838308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247838308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> fee1-dead <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247838308">(Aug 01 2021 at 03:46)</a>:</h4>
<p>You can wait for the const_trait_impl feature so you can use the index syntax in a const context.</p>



<a name="247871834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/subslices%20in%20const/near/247871834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/subslices.20in.20const.html#247871834">(Aug 01 2021 at 18:51)</a>:</h4>
<p><span class="user-mention" data-user-id="260325">@Chris Denton</span> Thank you so much for that! That was the final piece in the puzzle for me of validating ASN.1 tags entirely at <strong>compile-time</strong> <span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span>. For anyone interested here is the commit. If you're not familiar with ASN.1, it essentially has an extra ID on types and some types contain sets of IDs and this recurses which essentially forms a tree, and with <code>slice_range</code> I'm now able to recursively traverse a tree of these tags at <code>const</code> time, and check that every entry is unique. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span>  <a href="https://github.com/XAMPPRocky/rasn/commit/b2f0220d0f99b9187a3ad0aba0916b61236281d8#diff-1bb2a920eac967029f441f50ce32909b053d62dc865ca63adadf767699980d33">https://github.com/XAMPPRocky/rasn/commit/b2f0220d0f99b9187a3ad0aba0916b61236281d8#diff-1bb2a920eac967029f441f50ce32909b053d62dc865ca63adadf767699980d33</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>